<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">维保类别名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="name" class="layui-input" type="text" placeholder="请输入维保类别名称" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="maintenanceTypeAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">

                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="maintenanceTypeTableInfo" lay-filter="maintenanceTypeTableInfo"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="maintenanceTypeTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
<!--    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
</script>


<!-- 表单弹窗 -->
<script type="text/html" id="maintenanceTypeForm">
    <form lay-filter="maintenanceTypeForm" class="layui-form model-form">
        <input name="id" type="hidden"/>

        <div class="layui-form-item">
            <label class="layui-form-label">类别性质：</label>
            <div class="layui-input-block">
                <select id="taskNature" name="taskNature" lay-filter="taskNature" class="common-select-search">
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">类别名称：</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入类别名称" type="text" class="layui-input" lay-verify="required" required autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">类别编码：</label>
            <div class="layui-input-block">
                <input name="no" placeholder="请输入类别编码" type="text" class="layui-input" lay-verify="required" required autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">处置部门：</label>
            <div class="layui-input-block">
                <input id="departmentId" name="departmentId" type="hidden"/>
                <div id="selectDepartmentType"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">责任岗位：</label>
            <div class="layui-input-block">
                <select id="postIdSearchMain" name="postId" lay-verify="required" required lay-search></select>
            </div>
        </div>
        <div class="layui-form-item" >
            <label class="layui-form-label">描述：</label>
            <div class="layui-input-block">
                <textarea id="remark" name="remark" placeholder="维保类别描述" class="layui-textarea" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否启用：</label>
            <div class="layui-input-block">
                <input name="isDeleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="maintenanceTypeFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<script type="text/html" id="materialNavTemplate">
    {{# layui.each(d, function(index, item){ }}
    <div class="nav-cell {{# if(item.isShow){ }}nav-active{{# } }}" data-value="{{item.labelValue}}"><span>{{item.labelName}}</span></div>
    {{# }); }}
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form','table', 'laydate', 'admin' ,'laytpl','tableTreeDj', 'config'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;


        // 渲染表格
        var insTb = table.render({
            elem: '#maintenanceTypeTableInfo',
            url: config.base + 'enterprise/device/admin/maintenanceType/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号', fixed: 'left'}
                ,{align: 'center',field: 'no', align:'center', title: '类别编码', fixed: 'left'}
                ,{align: 'center',field: 'name', align:'center', title: '名称'}
                ,{align: 'center',field: 'taskNatureName', align:'center', title: '性质'}
                ,{align: 'center',field: 'departmentName', align:'center', title: '部门'}
                ,{align: 'center',field: 'postName', align:'center', title: '岗位'}
                ,{align: 'center',field: 'remark', align:'center', title: '描述'}
                ,{align: 'center',field: 'isDeleted', align:'center', title: '是否启用',templet:function (d) {
                    if(d.isDeleted){
                        return '<input name="isDeleted" type="checkbox" lay-skin="switch" value="'+d.id+'" checked="'+d.isDeleted+'" lay-filter="isDeleted"  lay-text="启用|停用"/>';
                    }else{
                        return '<input name="isDeleted" type="checkbox" lay-skin="switch" value="'+d.id+'" lay-filter="isDeleted"   lay-text="启用|停用"/>';
                    }

                    }}
                ,{align: 'center',field: 'creatorName', align:'center', title: '创建人'}
                ,{align: 'center',field: 'createTime', align:'center', title: '创建时间', width: 180}
                ,{align: 'center', toolbar: '#maintenanceTypeTableBar', title: '操作', width: 150, fixed: 'right'}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(maintenanceTypeTableInfo)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            }
            // else if (obj.event === 'del') { // 删除
            //     doDelete(obj);
            // }
        });

        form.on('switch(isDeleted)',function(obj){
            if(obj.elem.checked){
                enable(obj.value)
            }else{
                disable(obj.value)
            }
        })

        function enable(id){
            admin.req('enterprise/device/admin/maintenanceType/enable', {id:id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('maintenanceTypeTableInfo');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        function disable(id){
            admin.req('enterprise/device/admin/maintenanceType/disable', {id:id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('maintenanceTypeTableInfo');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        //监听排序
        table.on('sort(maintenanceTypeTableInfo)', function(obj){
            table.reload('maintenanceTypeTableInfo', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            console.log("data",data);
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', data => {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#maintenanceTypeAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '540px',
                offset: '165px',
                title: data ? '修改维修保养类型' : '添加维修保养类型',
                content: $('#maintenanceTypeForm').html(),
                success: function () {

                    let taskNature = "";
                    form.val('maintenanceTypeForm', data);
                    if (data){
                        admin.initDataSelectTree(true, 'user/admin/role/getTreeData', null, 'selectDepartmentType', data.departmentId, "部门", "departmentId", function (data) {
                            // 获取部门ID
                            let departmentId = data.change[0].value;
                            //赋值部门id
                            $('#departmentId').val(data.change[0].value);
                            $('#postIdSearchMain').empty();
                            //部门下的岗位列表
                            admin.initDataSelect("user/admin/role/getPostData", {roleId: departmentId}, "postIdSearchMain", data.postId, "岗位", null, null);
                        });
                        admin.initDataSelect("user/admin/role/getPostData", {roleId: data.departmentId}, "postIdSearchMain", data.postId, "岗位", null, null);

                    }else{
                        //部门列表
                        admin.initDataSelectTree(true, 'user/admin/role/getTreeData', null, 'selectDepartmentType', null, "部门", "departmentId", function (data) {
                            // 获取部门ID
                            let departmentId = data.change[0].value;
                            //赋值部门id
                            $('#departmentId').val(data.change[0].value);
                            $('#postIdSearchMain').empty();
                            //部门下的岗位列表
                            admin.initDataSelect("user/admin/role/getPostData", {roleId: departmentId}, "postIdSearchMain", null, "岗位", null, null);
                        });
                    }

                    //渲染下拉
                    form.render('select');

                    //设置默认值
                    form.render();
                    //选择框事件
                    form.on('select(taskNature)', function(data){
                        taskNature = data.value;
                    });
                    if (data!=null){
                        taskNature = data.taskNature;
                    }
                    // 表单提交事件
                    form.on('submit(maintenanceTypeFormSubmit)', function (d) {
                        layer.load(2);

                        var isDeleted=$("form input[name='isDeleted']").prop("checked");
                        if(!isDeleted){
                            d.field.isDeleted=false;
                        }else{
                            d.field.isDeleted=true;
                        }
                        d.field.taskNature = taskNature;
                        let params = {
                            ...d.field
                        }
                        //新增修改
                        admin.req(data ? 'enterprise/device/admin/maintenanceType/update' : 'enterprise/device/admin/maintenanceType/add', JSON.stringify(params), function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('maintenanceTypeTableInfo');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST', true, 'application/json; charset=utf-8');
                        return false;
                    });
                }
            });
            //查询设备类型
            admin.getDictList("taskNature", "task_nature", data ? data.taskNature : null, "类别性质");
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/device/admin/maintenanceType/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

    });
</script>